package org.mspring.mlog.service.impl;

import java.util.Date;

import org.mspring.mlog.entity.Log;
import org.mspring.mlog.service.LogService;
import org.mspring.platform.dao.hibernate.GenericDao;
import org.mspring.platform.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author Gao Youbo
 * @since 2013-06-18 16:34:42
 */
@Service
@Transactional
public class LogServiceImpl extends GenericDao<Log, java.lang.Long> implements LogService {

    @Override
    public Log save(Log object) {
        // TODO Auto-generated method stub
        if (object.getActionTime() == null) {
            object.setActionTime(new Date());
        }
        return super.save(object);
    }

    @Override
    public void deleteLogs(int days) {
        // TODO Auto-generated method stub
        String hql = "delete Log log ";
        if (days > 0) {
            Date date = DateUtils.addDays(new Date(), -days);
            String dateStr = DateUtils.format(date, DateUtils.YYYY_MM_DD_HH_MM_SS);
            hql += "where log.actionTime < '" + dateStr + "'";
        }
        executeUpdate(hql);
    }

}
